package model;

import Bean.Customer;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;

public class CustomerManagerModel extends RootModel
{

    private Customer Customer = new Customer();

    public ResultSetTableModel getAllCustomerCategory()
    {
        try
        {
            ConnectDB.connect();
            state = ConnectDB.getState();
            res = state.executeQuery(" Select * from customer");
            rstm = new ResultSetTableModel(res, 2);
            ConnectDB.close();

        }
        catch (SQLException ex)
        {
            Logger.getLogger(CustomerManagerModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            return rstm;
        }

    }

    public ResultSetTableModel filterNameCustomer(String key)
    {
        try
        {
            ConnectDB.connect();
            state = state = ConnectDB.getState();
            res = state.executeQuery("select * From Customer where `Full Name` like '%" + key + "%'");
            rstm = new ResultSetTableModel(res, 2);
            ConnectDB.close();

        }
        catch (SQLException ex)
        {
            Logger.getLogger(CustomerManagerModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            return rstm;
        }


    }

    public ResultSetTableModel filterPhoneCustomer(String key)
    {
        try
        {
            ConnectDB.connect();
            state = state = ConnectDB.getState();
            res = state.executeQuery("select * From Customer where `Phone` like '%" + key + "%'");
            rstm = new ResultSetTableModel(res, 2);
            ConnectDB.close();

        }
        catch (SQLException ex)
        {
            Logger.getLogger(CustomerManagerModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            return rstm;
        }
    }

    public ResultSetTableModel filterCMTNDCustomer(String key)
    {
        try
        {
            ConnectDB.connect();
            state = state = ConnectDB.getState();
            res = state.executeQuery("select * From Customer where `Identify Card` like '%" + key + "%'");
            rstm = new ResultSetTableModel(res, 2);
            ConnectDB.close();

        }
        catch (SQLException ex)
        {
            Logger.getLogger(CustomerManagerModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            return rstm;
        }

    }

    public boolean editCustomer(Customer Customer)
    {
        ConnectDB.connect();
        try
        {
            prst = ConnectDB.getPrst("UPDATE customer SET `Full Name`=?,Birthday=?,`Identify Card`=?,`Gender`=?,`Phone`=?,`Addrdess`=? WHERE `ID`=?;");
            prst.setString(1, Customer.getName());
            prst.setInt(2, Customer.getBrithday());
            prst.setString(3, Customer.getCMTND());
            prst.setString(4, Customer.getGender());
            prst.setLong(5, Customer.getPhone());
            prst.setString(6, Customer.getAdress());
            prst.setInt(7, Customer.getId());
            if (prst.executeUpdate() > 0)
            {
                return true;
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(CustomerManagerModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            ConnectDB.close();
        }
        return false;

    }

    public boolean addCustomer(Customer Customer)
    {
        ConnectDB.connect();
        try
        {
            prst = ConnectDB.getPrst("Insert into customer(`Full Name`,`Birthday`,`Identify Card`,`Gender`,`Phone`,`Addrdess`) value(?,?,?,?,?,?);");
            prst.setString(1, Customer.getName());
            prst.setInt(2, Customer.getBrithday());
            prst.setString(3, Customer.getCMTND());
            prst.setString(4, Customer.getGender());
            prst.setLong(5, Customer.getPhone());
            prst.setString(6, Customer.getAdress());

            prst.executeUpdate();

        }
        catch (SQLException ex)
        {
            Logger.getLogger(RoomManagerModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            ConnectDB.close();
        }
        return false;
    }

    public Customer getCustomer(int ID)
    {
        ConnectDB.connect();
        try
        {
            prst = ConnectDB.getPrst("SELECT * FROM customer WHERE ID =?");
            prst.setInt(1, ID);
            ResultSet rs = prst.executeQuery();
            while (rs.next())
            {
                Customer.setName(rs.getString("Full Name"));
                Customer.setBrithday(rs.getInt("Birthday"));
                Customer.setCMTND(rs.getString("Identify Card"));
                Customer.setGender(rs.getString("gender"));
                Customer.setPhone(rs.getLong("Phone"));
                Customer.setAdress(rs.getString("Addrdess"));
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(CustomerManagerModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            ConnectDB.close();
        }
        return Customer;
    }

    public boolean deleteCustomer(int ID)
    {
        ConnectDB.connect();
        try
        {
            prst = ConnectDB.getPrst("DELETE FROM customer WHERE ID=? ");
            prst.setInt(1, ID);
            if (prst.executeUpdate() > 0)
            {
                return true;
            }
        }
        catch (SQLException ex)
        {
            Logger.getLogger(RoomManagerModel.class.getName()).log(Level.SEVERE, null, ex);
        }
        finally
        {
            ConnectDB.close();
        }
        return false;
    }

    public boolean TestInforCustomer(int ID) throws SQLException
    {
        ConnectDB.connect();
        String Name = "";
        prst = ConnectDB.getPrst("select `Full Name` from Customer,reservation where (Customer.ID =?) and(reservation.CustomerID=?) and(Customer.ID=reservation.CustomerID)");
        prst.setInt(1, ID);
        prst.setInt(2, ID);
        ResultSet rs = prst.executeQuery();
        while (rs.next())
        {
            Name = rs.getString("Full Name");
        }
        ConnectDB.close();

        if (!"".equals(Name))
        {
            return false;
        }
        else
        {
            return true;
        }
    }
}
